package ace.cmp.util;

import java.awt.*;
import org.apache.commons.lang3.StringUtils;

/**
 * @author caspar
 * @date 2023/3/22 15:11
 */
public final class AceColorUtils {
  /**
   * for example: #00000000 or #000000 or 192,192,192,192 or 192,192,192 ,RGB or RGBA  格式颜色字符串转{@link Color}
   *
   * @param value support RGB or RGBA ,for example: #00000000 or #000000 or 192,192,192,192 or 192,192,192
   * @return
   */
  public static Color decode(String value, Color defaultColor) {
    if (StringUtils.isEmpty(value)) {
      return defaultColor;
    } else if (value.startsWith("#")) {
      if (value.length() == 7) {
        Color color = Color.decode(value);
        return color != null ? color : defaultColor;
      } else if (value.length() == 9) {
        try {
          String rs = value.substring(1, 3);
          String gs = value.substring(3, 5);
          String bs = value.substring(5, 7);
          String as = value.substring(7, 9);
          Integer r = Integer.valueOf(rs, 16);
          Integer g = Integer.valueOf(gs, 16);
          Integer b = Integer.valueOf(bs, 16);
          Integer a = Integer.valueOf(as, 16);
          return new Color(r, g, b, a);
        } catch (Exception ex) {
          return defaultColor;
        }
      } else {
        return defaultColor;
      }
    } else if (value.contains(",")) {
      String[] intString = value.split(",");
      if (intString.length != 3 && intString.length != 4) {
        return defaultColor;
      }
      try {
        Integer r = Integer.valueOf(intString[0]);
        Integer g = Integer.valueOf(intString[1]);
        Integer b = Integer.valueOf(intString[2]);
        if (intString.length == 4) {
          Integer a = Integer.valueOf(intString[3]);
          return new Color(r, g, b, a);
        } else {
          return new Color(r, g, b);
        }
      } catch (Exception ex) {
        return defaultColor;
      }
    } else {
      return defaultColor;
    }
  }

  public static Color decode(String value) {
    return decode(value, null);
  }

  /**
   * {@link Color} 转 #000000格式颜色字符串
   *
   * @param color
   * @return #000000格式颜色
   */
  public static String encodeRgbHex(Color color) {
    int r = color.getRed();
    int g = color.getGreen();
    int b = color.getBlue();
    return String.format("#%02X%02X%02X", r, g, b);
  }

  /**
   * {@link Color} 转 #00000000格式颜色字符串
   *
   * @param color
   * @return #000000格式颜色
   */
  public static String encodeRgbaHex(Color color) {
    int r = color.getRed();
    int g = color.getGreen();
    int b = color.getBlue();
    int a = color.getAlpha();
    return String.format("#%02X%02X%02X%02X", r, g, b, a);
  }

  /**
   * {@link Color} 转  192,192,192 格式颜色字符串
   *
   * @param color
   * @return #000000格式颜色
   */
  public static String encodeRgb(Color color) {
    int r = color.getRed();
    int g = color.getGreen();
    int b = color.getBlue();
    return String.format("%s,%s,%s", r, g, b);
  }

  /**
   * {@link Color} 转  192,192,192 格式颜色字符串
   *
   * @param color
   * @return #000000格式颜色
   */
  public static String encodeRgba(Color color) {
    int r = color.getRed();
    int g = color.getGreen();
    int b = color.getBlue();
    int a = color.getAlpha();
    return String.format("%s,%s,%s,%s", r, g, b, a);
  }
}
